/**
 * @fileName: base.js
 * @author: coolboy11058@126.com
 * @time: 2017/5/8
 * @disc: axios 全局配置
 */
import Qs from 'qs';
import axios from 'axios';
import router from '@/router';
import { delCookie, evnConfigPath } from "@/assets/utils";

let instance = axios.create({
    url: '',
    method: 'post',
    baseURL: evnConfigPath('baseURL'),
    transformRequest: [function(data) {
        let serizeData = Qs.stringify(data);
        return serizeData;
    }],
    transformResponse: [function(data) {
        let serizeData = Qs.parse(data);
        return serizeData;
    }],
    withCredentials: true,
    // crossDomain:true,
    responseType: 'json',
    timeout: 100000
});
/**
 * 添加Cookie请求头信息
 */

instance.interceptors.request.use(config => {
    // config.withCredentials = true;
    return config;
}, e => {
    return Promise.reject(e);
});
/**
 * 添加拦截器响应
 */
instance.interceptors.response.use(res => {
    if (!res.data.success) {
        // this.$message({
        //     type: 'error',
        //     message: res.data.msg
        // })
    }
    return res;
}, e => {
    if (e.response) {
        switch (e.response.status) {
            case 401:
            case 403:
                delCookie('userInfo');
                delCookie('agentCode');
                delCookie('navName')
                router.push({ name: 'login' });
                break;
        }
    }
    return Promise.reject(e);
});
export default instance;